package com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.persenter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hzdesk.media.matvector.koreabatch.client.my_paperless.R;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.adapter.GuidePageFragmentAdapter;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.fragment.Fragment_view.GuidePageOneFragment;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.fragment.Fragment_view.GuidePageThreeFragment;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.fragment.Fragment_view.GuidePageTwoFragment;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.contract.IGuidePageContract;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/8/9.
 */

public class GuidePagePersenter implements IGuidePageContract.IGuidePagePersenter{
    private String TAG=GuidePagePersenter.class.getSimpleName();
    private IGuidePageContract.IGuidePageView iGuidePageView;
    private GuidePageFragmentAdapter guidePageFragmentAdapter;
    private List<Fragment> fragmentList;
    private List<TextView> pointList;
    private ViewPager viewPager;
    private Context mContext;

    public GuidePagePersenter(IGuidePageContract.IGuidePageView iGuidePageView) {
        this.iGuidePageView = iGuidePageView;
    }

    @Override
    public void initView(ViewPager viewPager, FragmentManager fragmentManager,Context context) {
        mContext=context;
        this.viewPager=viewPager;
        fragmentList=initList();
        guidePageFragmentAdapter=new GuidePageFragmentAdapter(fragmentManager,fragmentList);
        this.viewPager.setAdapter(guidePageFragmentAdapter);
        this.viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.d(TAG, "onPageSelected: "+position);
                changePoint(pointList.get(position));


            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void initPoints(TextView view1, TextView view2, TextView view3) {
        pointList=new ArrayList<>();
        pointList.add(view1);
        pointList.add(view2);
        pointList.add(view3);
    }


    private List<Fragment> initList(){
        List<Fragment> list=new ArrayList<>();
        list.add(new GuidePageOneFragment());
        list.add(new GuidePageTwoFragment());
        list.add(new GuidePageThreeFragment());
        return list;
    }

    private void changePoint(TextView view){
        refreshPoint();
        ViewGroup.LayoutParams layoutParams=view.getLayoutParams();
        int width =(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 17, mContext.getResources().getDisplayMetrics());
        int height =(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,8,mContext.getResources().getDisplayMetrics());
        layoutParams.width=width;
        layoutParams.height=height;
        view.setLayoutParams(layoutParams);
        view.setBackgroundResource(R.drawable.guide_page_point_light);

    }
    //全部变成灰色
    private void refreshPoint(){
        for (TextView textView:pointList){
            ViewGroup.LayoutParams layoutParams=textView.getLayoutParams();
            int width =(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, mContext.getResources().getDisplayMetrics());
            int height =(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,8,mContext.getResources().getDisplayMetrics());
            layoutParams.width=width;
            layoutParams.height=height;
            textView.setLayoutParams(layoutParams);
            textView.setBackgroundResource(R.drawable.guide_page_point_dark);
        }

    }
}
